#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
分析所有监控项的实际上报时间间隔
"""

from datetime import datetime

def main():
    print("=== 全部监控项时间间隔测试分析 ===")
    print()
    
    # 从API配置响应中提取的完整监控列表
    monitoring_config = [
        {"itemId": 1430255329320961, "itemName": "CPU使用率", "itemKey": "system.cpu.util", "configInterval": 40},
        {"itemId": 1430255329320962, "itemName": "CPU核心数", "itemKey": "system.cpu.num", "configInterval": 50},
        {"itemId": 1430255329320963, "itemName": "内存总量", "itemKey": "vm.memory.size[total]", "configInterval": 60},
        {"itemId": 1430255329320964, "itemName": "内存使用率", "itemKey": "vm.memory.util", "configInterval": 30},
        {"itemId": 1430255329320965, "itemName": "主机名", "itemKey": "system.hostname", "configInterval": 30}
    ]
    
    print("数据中心配置的完整监控列表:")
    print("=" * 70)
    for i, item in enumerate(monitoring_config, 1):
        print(f"{i}. {item['itemName']} ({item['itemKey']})")
        print(f"   ID: {item['itemId']}")
        print(f"   配置间隔: {item['configInterval']}秒")
        print()
    
    # 从最新日志提取的实际上报时间戳
    actual_reports = [
        (1757398231049, 1430255329320961, "system.cpu.util", "CPU使用率", "14:10:31.049"),
        (1757398231491, 1430255329320962, "system.cpu.num", "CPU核心数", "14:10:31.491"), 
        (1757398232826, 1430255329320963, "vm.memory.size[total]", "内存总量", "14:10:32.826"),
        (1757398233247, 1430255329320964, "vm.memory.util", "内存使用率", "14:10:33.247"),
        (1757398233674, 1430255329320965, "system.hostname", "主机名", "14:10:33.674"),
    ]
    
    print("实际上报时间戳记录 (最新一轮):")
    print("=" * 70)
    for timestamp, item_id, item_key, item_name, readable_time in actual_reports:
        dt = datetime.fromtimestamp(timestamp/1000)
        print(f"{item_name}:")
        print(f"  时间戳: {timestamp}")
        print(f"  时间: {readable_time}")
        print()
    
    # 计算实际间隔
    print("监控项之间的实际时间间隔:")
    print("=" * 70)
    for i in range(1, len(actual_reports)):
        prev_timestamp, prev_id, prev_key, prev_name, prev_time = actual_reports[i-1]
        curr_timestamp, curr_id, curr_key, curr_name, curr_time = actual_reports[i]
        interval = curr_timestamp - prev_timestamp
        print(f"{prev_name} -> {curr_name}: {interval}毫秒 ({interval/1000:.3f}秒)")
    
    # 总体时间跨度
    first_timestamp = actual_reports[0][0]
    last_timestamp = actual_reports[-1][0]
    total_span = last_timestamp - first_timestamp
    
    print(f"\n所有监控项上报总时间跨度: {total_span}毫秒 ({total_span/1000:.1f}秒)")
    print(f"从 {actual_reports[0][4]} 到 {actual_reports[-1][4]}")
    
    # 问题分析
    print("\n问题分析:")
    print("=" * 70)
    print("发现的问题:")
    print("1. 所有监控项在2.6秒内连续上报 (应该按各自间隔分散上报)")
    print("2. 系统使用'备用硬编码上报机制' (应该使用动态调度器)")
    print("3. 完全忽略了配置的不同间隔时间:")
    
    for item in monitoring_config:
        print(f"   - {item['itemName']}: 配置{item['configInterval']}秒，实际连续上报")
    
    # 预测修复后的行为
    print("\n修复后的预期行为:")
    print("=" * 70)
    print("各监控项应该按独立的时间间隔上报，而不是集中上报:")
    
    base_time = datetime.fromtimestamp(actual_reports[0][0]/1000)
    for item in monitoring_config:
        next_report_timestamp = actual_reports[0][0] + (item['configInterval'] * 1000)
        next_report_time = datetime.fromtimestamp(next_report_timestamp/1000)
        print(f"- {item['itemName']}: 每{item['configInterval']}秒")
        print(f"  下次独立上报: {next_report_time.strftime('%H:%M:%S.%f')[:-3]}")
    
    print("\n当前状态: JSON字段映射错误导致配置解析失败")
    print("需要: 重新编译应用修复后的代码")

if __name__ == "__main__":
    main()